/*
 * Created by 岱青海蓝信息系统(北京)有限公司 on 17-6-15 下午1:29
 * Copyright (c) 2017. All rights reserved.
 */

package com.example.dqhl.jdmetro.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.example.dqhl.jdmetro.R;
import com.example.dqhl.jdmetro.base.BaseFragment;
import com.example.dqhl.jdmetro.bean.live.LiveTab;
import com.example.dqhl.jdmetro.config.Config;
import com.example.dqhl.jdmetro.fragment.Live.LiveCommonFragment;
import com.example.dqhl.jdmetro.utils.Dlog;
import com.example.dqhl.jdmetro.utils.JsonUtils;
import com.example.dqhl.jdmetro.utils.Toastutils;
import com.example.dqhl.jdmetro.widget.PagerSlidingTabStrip;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * description:直播汇
 * autour:guanluocang
 * date:17/6/15 13:32
 */
public class LiveFragment extends BaseFragment {

    private Context context;
    @Bind(R.id.live_viewPager)
    ViewPager live_viewPager;
    @Bind(R.id.tabs)
    PagerSlidingTabStrip tabs;
    //头部Tab
    private List<LiveTab> mLiveTabsList = new ArrayList<>();
    private List<String> mTitle = new ArrayList<>();
    private List<Fragment> mFragmentList = new ArrayList<>();

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public View initView(LayoutInflater inflater) {
        rootView = inflater.inflate(R.layout.fragment_live, null);
        context = getActivity();
        ButterKnife.bind(this, rootView);
        initTitleBar(rootView, R.string.txt_live);
        setLeftBackVisibility(rootView, View.GONE);
        return rootView;
    }

    @Override
    public void initData() {
        //设置可以根据viewpage横向滚懂
//        LinearLayout linearLayout = (LinearLayout) live_tabLayout.getChildAt(0);
//        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
//        linearLayout.setDividerDrawable(ContextCompat.getDrawable(getActivity(),
//                R.drawable.layout_tabout_divid));
//        linearLayout.setDividerPadding(CommonUtil.dip2px(getActivity(), 15));
//        live_tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        initTopTab();

    }

    @Override
    public void initListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
    }

    /**
     * 顶部Tab
     */
    public void initTopTab() {
        if (mFragmentList != null && mFragmentList.size() > 0) {
            return;
        }

        final RequestParams params = new RequestParams(Config.live_topTab);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Dlog.e(result);
                int errCode = JsonUtils.getErrCode(result);
                String data = JsonUtils.getData(result);
                String errMsg = JsonUtils.getErrMsg(result);
                if (errCode == 200) {
                    mLiveTabsList = JSON.parseArray(data, LiveTab.class);
                    for (int i = 0; i < mLiveTabsList.size(); i++) {
                        mTitle.add(mLiveTabsList.get(i).getName());
                    }
                    Dlog.e(mTitle + "");
                    for (int i = 0; i < mTitle.size(); i++) {
                        LiveCommonFragment liveCommonFragment_new = new LiveCommonFragment();
                        Bundle bundle = new Bundle();
                        bundle.putString("theme_id", mLiveTabsList.get(i).getId());
                        liveCommonFragment_new.setArguments(bundle);
                        mFragmentList.add(liveCommonFragment_new);
                    }
                    setPageTitle();
                } else {
                    Toastutils.show(errMsg);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


    /*设置Title样式*/
    private void setPageTitle() {
        //viewPager 记载adapter
        live_viewPager.setAdapter(new MyAdapter(getFragmentManager()));
        live_viewPager.setCurrentItem(0, true);
//        live_viewPager.setOffscreenPageLimit(10);//保存viewpage数据不被回收
        tabs.setViewPager(live_viewPager);
        tabs.setIndicatorHeight(5);//设置下划线宽度
        tabs.setUnderlineHeight(2);//设置底部边框宽度
        tabs.setDividerColor(getResources().getColor(R.color.color_pst_divider_color)); //中间竖线颜色
        tabs.setIndicatorColor(getResources().getColor(R.color.colorPrimary));// 下划线颜色
        tabs.setSelectedTextColor(getResources().getColor(R.color.colorPrimary));//文字选中颜色
        tabs.setTextColor(getResources().getColor(R.color.color_black));//没有选中时文字颜色
        tabs.setTextSize(getResources().getDimensionPixelSize(R.dimen.dimen_20));//字体大小
        tabs.setShouldExpand(true);
        tabs.setTabBackground(R.drawable.background_tab);

    }

    /**
     * 在界面切换的时候会调用这个方法
     * true 表示可见
     * FALSE 表示不可见
     *
     * @param isVisibleToUser
     */
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {//表示界面可见
//            if (mNoHorizontalScrollView != null) {// 之所以判断是否为空，
//                Log.i(TAG, "setUserVisibleHint: mTitle=" + mTitle + "  " + " isVisibleToUser=" +
//                        isVisibleToUser + "mScrollY=" + mScrollY);
//                //                mNoHorizontalScrollView.setDescendantFocusability(ViewGroup
//                // .FOCUS_BLOCK_DESCENDANTS);
//            }

        } else {// 表示界面不可见
//            if (mNoHorizontalScrollView != null) {
//                mScrollX = mNoHorizontalScrollView.getScrollX();
//                mScrollY = mNoHorizontalScrollView.getScrollY();
//                Log.i(TAG, "setUserVisibleHint: mTitle=" + mTitle + "  " + " isVisibleToUser=" +
//                        isVisibleToUser + "mScrollY=" + mScrollY);
//
//            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    public class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);
        }
    }
}
